﻿namespace MomentumPlugin.Common
{
	internal class StrategyControlVM : Plugins.Base.Common.StrategyControlVMBase
	{
		#region Properties

		public new StrategyControlData Data
		{
			get
			{
				return (StrategyControlData)base.Data;
			}
		}

		public decimal MinVariation
		{
			get
			{
				return this.Data.MinVariation;
			}
			set
			{
				this.Data.MinVariation = value;
			}
		}

		public int MaxSeconds
		{
			get
			{
				return this.Data.MaxSeconds;
			}
			set
			{
				this.Data.MaxSeconds = value;
			}
		}

		public int Volume
		{
			get
			{
				return this.Data.Volume;
			}
			set
			{
				this.Data.Volume = value;
			}
		}

		public ExitMode ExitMode
		{
			get
			{
				return this.Data.ExitMode;
			}
			set
			{
				this.Data.ExitMode = value;
			}
		}

		public decimal MinWin
		{
			get
			{
				return this.Data.MinWin;
			}
			set
			{
				this.Data.MinWin = value;
			}
		}

		public decimal MaxLoss
		{
			get
			{
				return this.Data.MaxLoss;
			}
			set
			{
				this.Data.MaxLoss = value;
			}
		}

		#endregion

		#region Public Methods

		public StrategyControlVM() : base(new StrategyControlData())
		{
		}

		protected override string ValidateField(string fieldName)
		{
			string result = null;

			if (fieldName == "MinVariation")
			{
				if (this.MinVariation <= 0)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "The min variation has to be greater than 0";
				}
			}
			else if (fieldName == "MaxSeconds")
			{
				if (this.MaxSeconds <= 0)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "The maximum seconds has to be greater than 0";
				}
			}
			else if (fieldName == "Volume")
			{
				if (this.Volume <= 0)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "The volume has to be greater than 0";
				}
			}
			else if (fieldName == "ExitMode")
			{
				 if (this.ExitMode == ExitMode.None)
				 {
					 base.ValidatedErrors[fieldName] = true;
					 result = "The mode has to be set";
				 }
				 else
				 {
					 // Force validation of properties
					 base.OnPropertyChanged("MinWin");
					 base.OnPropertyChanged("MaxLoss");
				 }
			}
			else if (fieldName == "MinWin")
			{
				if (this.MinWin <= 0 && this.ExitMode == ExitMode.OnFixedAmount)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "The minimum win has to be greater than 0";
				}
			}
			else if (fieldName == "MaxLoss")
			{
				if (this.MaxLoss < 0 && this.ExitMode == ExitMode.OnFixedAmount)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "The max loss has to be 0 or more";
				}
			}

			return result;
		}

		#endregion
	}
}
